package com.onemore.task.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONObject;
import com.onemore.task.JdbcGlobal;
import com.onemore.task.OneJdbc;

public class ModelJorder extends JdbcGlobal{

	public final static String jorder_field[] = new String[] {"DJBH","RQ","DJXZ","FPLX","LXDJ",
    		"DM1","DM2","DM2_1","DM4","QDDM","QYDM","YGDM","SL","JE","BZJE","TJ","XC",
    		"JZ","JZRQ","JS","JSRQ","SP","SPRQ","ZDR","JZR","SPR","LLR","YXRQ","RQ_1",
    		"RQ_2","RQ_4","BZ","BYZD1","BYZD2","BYZD5","BYZD9","BYZD11","BYZD12",
    		"BYZD13","ZS","ZSR","ZSRQ","DM5","BYZD16","BYZD17"};//"BYZD6",
	static public boolean isExist(String DJBH) throws SQLException
	{
		if(search("JORDER", "DJBH='"+DJBH+"'")){
    		return true;
    	}
		return false;
	}
	/**
     * 插入商品进货通知单主表 主键：DJBH
     * @param json
     * @return
     * @throws SQLException 
     */
	static public boolean insert_jorder(JSONObject json) throws SQLException
    {
		String fieldStr = "";
    	String valuesStr = "";
    	String updateStr = "";
    	List<String> listFields = new ArrayList<String>();
    	int i = 0;
    	for(i=0;i<jorder_field.length;i++) {
    		if(json.containsKey(jorder_field[i]))
    			listFields.add(jorder_field[i]);
    	}
    	for(i=0;i<listFields.size()-1;i++) {
    		fieldStr += listFields.get(i)+",";
    		valuesStr += "'"+json.getString(listFields.get(i))+"',";
    		updateStr += listFields.get(i)+"='"+json.getString(listFields.get(i))+"',";
    	}
		fieldStr += listFields.get(i);
		valuesStr += "'"+json.getString(listFields.get(i))+"'";
		updateStr += listFields.get(i)+"='"+json.getString(listFields.get(i))+"'";
		//delete("JORDER",jorder_field[0]+"='"+json.getString(jorder_field[0])+"'");
    	String sql = "";
    	if(search("JORDER", jorder_field[0]+"='"+json.getString(jorder_field[0])+"'")) {
			updateDate("JORDER",jorder_field[0]+"='"+json.getString(jorder_field[0])+"'",updateStr);
		}else {
	        //查询测试
	        sql = "insert into JORDER ("+fieldStr+") values("+valuesStr+")";
	        Map<String, Object> map = null;
			Logger.debug(sql);
			getJdbcOperate().update(sql);
		}
		return true;
    }
}
